<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.9
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############

ob_start();


$VIEW->template = $this->dirname.'/templates/thickbox.tpl';

$pageid = (int) get('page');





$selectedGroups = array();
$selGroups = $DB->fetchAll("SELECT * FROM {$CONFIG->dbprefix}iusers_pages_to_groups WHERE page_id = '{$pageid}'");
foreach($selGroups as $k=>$v){
    $selectedGroups[] = $v['group_key'];
}



if(Antz_IntelliForm::submitted('iusers-page-permissions')){
    $selectedGroupsRev = array_flip($selectedGroups);
    $recursive = (bool) post('recursive', false);

    if($recursive){
        $pagesRecursive = JFX::getPageTree($pageid, $JFXUSER->workspace, true);
        foreach($pagesRecursive as $k=>$v){
            $DB->delete($CONFIG->dbprefix.'iusers_pages_to_groups', "page_id = '{$v['id']}'");
        }
    }

    foreach($_POST['groups'] as $k=>$v){
        if($recursive){
            IUsers_SetPagePermissions_RecurseGroup($pagesRecursive, $v);
        }

        if(in_array($v, $selectedGroups)){
            unset($selectedGroups[$selectedGroupsRev[$v]]);
            continue;
        }
        $v = $DB->escape($v);
        if($DB->countRows($CONFIG->dbprefix.'iusers_groups', "keyname = '{$v}'")==0) continue;
        $dbParams = array(
            'page_id' => $pageid,
            'group_key' => $v
        );
        $DB->insert($CONFIG->dbprefix.'iusers_pages_to_groups', $dbParams);

        

    }
    // tidy up leftover groups
    foreach($selectedGroups as $k=>$v){
        $DB->delete($CONFIG->dbprefix.'iusers_pages_to_groups', "group_key = '{$v}' AND page_id = '{$pageid}'");
    }
    JFX::addSuccess($this->lang('success_update_permissions'));
}

$selectedGroups = array();
$selGroups = $DB->fetchAll("SELECT * FROM {$CONFIG->dbprefix}iusers_pages_to_groups WHERE page_id = '{$pageid}'");
foreach($selGroups as $k=>$v){
    $selectedGroups[] = $v['group_key'];
}



$groupOpts = array();
$groups = $DB->fetchAll("SELECT * FROM {$CONFIG->dbprefix}iusers_groups ORDER BY keyname ASC");
foreach($groups as $k=>$v){
    $groupOpts[$v['keyname']] = $v['title'].' ['.$v['keyname'].']';
}



$formFields = array(
    array(
        'type' => 'select',
        'size' => 3,
        'name' => 'groups[]',
        'value' => $selectedGroups,
        'options' => $groupOpts,
        'multiple' => true,
        'label' => $this->lang('access_groups')
    ),
    array(
        'type' => 'checkbox',
        'name' => 'recursive',
        'label' => $this->lang('recurse_into_subpages'),
        'value' => 1,
        'checked' => (bool) post('recursive', false)
    ),
    array(
        'type' => 'submit',
        'name' => 'submitBtn',
        'value' => $this->lang('update'),
        'label' => '&nbsp;'
    )
);














echo '<h1>'.$this->lang('setting_permissions_for').$LANG->getContent('core', 'heading', $pageid).'</h1>';

echo JFX::makeRapidForm($formFields, $this->lang('set_access_permissions'), 'iusers-page-permissions');












$content = ob_get_contents();
ob_end_clean();

$SMARTY->assign('_CST_BODY', $content);



















function IUsers_SetPagePermissions_RecurseGroup($pagesRecursive, $groupKey){
    $DB = JFX::registry('db');
    $CONFIG = JFX::registry('config');



    foreach($pagesRecursive as $key=>$value){
        if($DB->countRows($CONFIG->dbprefix.'iusers_pages_to_groups', "page_id = '{$value['id']}' AND group_key = '{$groupKey}'")>0){
            continue;
        }

        $dbParams = array(
            'page_id' => $value['id'],
            'group_key' => $groupKey
        );
        $DB->insert($CONFIG->dbprefix.'iusers_pages_to_groups', $dbParams);
        if(count($value['subpages'])>0){
            IUsers_SetPagePermissions_RecurseGroup($value['subpages'], $groupKey);
        }
    }

}





